package com.modificial.shiro.controller;

import com.modificial.shiro.system.domain.SysPermission;
import com.modificial.shiro.system.service.PermissionService;
import com.modificial.shiro.system.vo.PermissionInfo;
import com.modificial.shiro.system.vo.PermissionTree;
import com.modificial.shiro.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author modificial
 * @date 2018/8/6 0006
 * @company modificial_org
 * @description 权限管理
 */
@RestController
@RequestMapping("/permission")
@Api(tags = "权限管理")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @PostMapping("/addPermission")
    @ApiOperation(value = "添加权限信息")
    public JsonResult addPermission(@RequestBody PermissionInfo permissionInfo){
        SysPermission sysPermission=new SysPermission();
        BeanUtils.copyProperties(permissionInfo,sysPermission);
       int count=permissionService.addPermission(sysPermission);
       if(count==1){
           return new JsonResult(HttpStatus.OK.value(),"权限信息添加成功",count);
       }
        return  new JsonResult(HttpStatus.OK.value(),"权限信息添加失败",0);
    }

    @PostMapping("/updatePermission")
    @ApiOperation(value = "更新权限信息")
    public JsonResult updatePermission(@RequestBody PermissionInfo permissionInfo){
        SysPermission sysPermission=new SysPermission();
        BeanUtils.copyProperties(permissionInfo,sysPermission);
        int count=permissionService.updatePermission(sysPermission);
        if(count==1){
            return new JsonResult(HttpStatus.OK.value(),"权限信息更新成功",count);
        }
        return  new JsonResult(HttpStatus.OK.value(),"权限信息更新失败",count);
    }
    @PostMapping("/deletePermission")
    @ApiOperation(value = "删除权限信息")
    public JsonResult deletePermission(@ApiParam(value = "权限id")@RequestParam String permissionId){
        int count=permissionService.deletePermissionById(permissionId);
        if(count==1){
            return new JsonResult(HttpStatus.OK.value(),"权限信息删除成功",count);
        }
        return  new JsonResult(HttpStatus.OK.value(),"权限信息删除失败",count);
    }
    @PostMapping("/findPermissions")
    @ApiOperation(value = "查询权限信息")
    public  JsonResult findPermissions(){
      List<PermissionTree>permissions= permissionService.listPermissions();
      if(permissions!=null&&!permissions.isEmpty()){
          JsonResult jsonResult=new JsonResult(HttpStatus.OK.value(),"权限信息查询成功",1);
          jsonResult.setData(permissions);
          return jsonResult;
      }
        return new JsonResult(HttpStatus.OK.value(),"权限信息查询失败",0);
    }
    @PostMapping("/findPermissionById")
    @ApiOperation(value = "根据权限id查询权限")
    public  JsonResult findPermissionById(@ApiParam(value = "权限id")@RequestParam String permissionId){
        SysPermission sysPermission=permissionService.findPermissionById(permissionId);
        if(sysPermission!=null){
            JsonResult jsonResult=new JsonResult(HttpStatus.OK.value(),"权限信息查询成功",1);
            jsonResult.setData(sysPermission);
            return jsonResult;
        }
        return  new JsonResult(HttpStatus.OK.value(),"权限信息查询失败",0);
    }
}
